/**************************
This function parse the color and flux information
and fills in arrays useful for the initialisation

*****************************/


//bunch of external 

extern vector<string> allfluxes;  
extern vector<string> color1;     
extern vector<string> color2;  
extern vector<int> color1_ind;     
extern vector<int> color2_ind;  
extern vector<float> allmincolor; 
extern vector<float> allmaxcolor; 
extern vector<float> allminflux; 
extern vector<float> allmaxflux; 
extern long which_fluxes;


void histo_initialise(string outflux, string outcolor, string outminflux, string outmaxflux,
		      string outmincolor, string outmaxcolor){
  
   
  size_t f1,f2,f3,end;
  int stop;
  string thisflux,thiscolor,tmpstr;
  vector<int> fluxid;


  //Here translate the color/fluxes in internal code
  //Table for fluxes:
  //1-nuv; 2-fuv; 3-bol; 4-qh0; 5-J; 6-H; 7-K; 8-SDSSu; 9-SDSSg; 10-SDSSr;
  //11-SDSSi; 12-SDSSz; 13-JohnU; 14-JohnB; 15-JohnV; 16-JohnR; 17-JohnI 
  
  
  
  //read through flux input string to find comma-delimited flux names and set the
  // which_fluxes bitmask

  //Start with fluxes
  f2=outflux.find(",");
  f1=0;
  end=outflux.size()-1;
  if(f2 > end) stop=1;
  else stop=0;

  
  while(f2){
    //extract substring
    thisflux=outflux.substr(f1,f2);
    
    //trim spaces
    size_t strtpos=thisflux.find_first_not_of(" ");
    size_t enpos=thisflux.find_last_not_of(" ");
    thisflux=thisflux.substr(strtpos,enpos-strtpos+1);
    
    outflux.assign(outflux.substr(f2+1,end));
     
    //update the bit mask for the fluxes
    allfluxes.push_back(thisflux);
    
    //check all the fluxes in the table
    if(thisflux.compare("NUV")==0){ 
      which_fluxes+=(long)pow((double)2,(double)1);
      fluxid.push_back(1);
    }
    if(thisflux.compare("FUV")==0){ 
      which_fluxes+=(long)pow((double)2,(double)2);
      fluxid.push_back(2);
    }
    if(thisflux.compare("BOL")==0){ 
      which_fluxes+=(long)pow((double)2,(double)3);
      fluxid.push_back(3);
    }
    if(thisflux.compare("QH0")==0){
      which_fluxes+=(long)pow((double)2,(double)4);
      fluxid.push_back(4);
    }
    if(thisflux.compare("J")==0){
      which_fluxes+=(long)pow((double)2,(double)5);
      fluxid.push_back(5);
    }
    if(thisflux.compare("H")==0){ 
      which_fluxes+=(long)pow((double)2,(double)6);
      fluxid.push_back(6);
    }
    if(thisflux.compare("K")==0){ 
      which_fluxes+=(long)pow((double)2,(double)7);
      fluxid.push_back(7);
    }
    if(thisflux.compare("SDSSU")==0){ 
      which_fluxes+=(long)pow((double)2,(double)8);
      fluxid.push_back(8);
    }
    if(thisflux.compare("SDSSG")==0){ 
      which_fluxes+=(long)pow((double)2,(double)9);
      fluxid.push_back(9);
    }
    if(thisflux.compare("SDSSR")==0){
      which_fluxes+=(long)pow((double)2,(double)10);
      fluxid.push_back(10);
    }
    if(thisflux.compare("SDSSI")==0){
      which_fluxes+=(long)pow((double)2,(double)11);
      fluxid.push_back(11);
    }
    if(thisflux.compare("SDSSZ")==0){
      which_fluxes+=(long)pow((double)2,(double)12);
      fluxid.push_back(12);
    }
    if(thisflux.compare("JOHNSONU")==0){
      which_fluxes+=(long)pow((double)2,(double)13);
      fluxid.push_back(13);
    }
    if(thisflux.compare("JOHNSONB")==0){
      which_fluxes+=(long)pow((double)2,(double)14);
      fluxid.push_back(14);
    }
    if(thisflux.compare("JOHNSONV")==0){
      which_fluxes+=(long)pow((double)2,(double)15);
      fluxid.push_back(15);
    }
    if(thisflux.compare("JOHNSONR")==0){
      which_fluxes+=(long)pow((double)2,(double)16);
      fluxid.push_back(16);
    }
    if(thisflux.compare("JOHNSONI")==0){
      which_fluxes+=(long)pow((double)2,(double)17);
      fluxid.push_back(17);
    }
  
    //update
    end=outflux.size()-1;
    f2=outflux.find(",");
    if(stop == 1) break;
    if(f2>end)stop=1;
  };
  
 //we now do the same for the colors. First we parse the colors
 // input string to find the names of all the fluxes used in the
 //colors and then we proceed to find the indices of the 
 //fluxes above which correspond to each flux used in the colors
  
  //do color
  f2=outcolor.find(",");
  f1=0;
  end=outcolor.size()-1;
  if(f2 > end) stop=1;
  else stop=0;
  
  while(f2){
    //extract substring
    thiscolor=outcolor.substr(f1,f2);

    //trim spaces
    size_t strtp=thiscolor.find_first_not_of(" ");
    size_t enp=thiscolor.find_last_not_of(" ");
    thiscolor=thiscolor.substr(strtp,enp-strtp+1);
    
    outcolor.assign(outcolor.substr(f2+1,end));
    
    //separate color
    f3=thiscolor.find("-");
    color1.push_back(thiscolor.substr(0,f3));
    color2.push_back(thiscolor.substr(f3+1,thiscolor.size()-1));
  
    //update
    end=outcolor.size()-1;
    f2=outcolor.find(",");
    if(stop == 1) break;
    if(f2>end)stop=1;
  };
    
  
  //Before going further check if color matches fluxes
  //and assign indexes
  int found;

  for(int i=0;i<(int)color1.size();i++){
    found=0;
    for(int j=0;j<(int)allfluxes.size();j++){
      if(color1[i] == allfluxes[j]){
	found=1;
	//assign index
	color1_ind.push_back(fluxid[j]);
      }
    }
    //if not found error
    if(found !=1){
      cout<<"HISTO_INIT: Error!!! To compute color with "<<color1[i]
	  <<" you must set the flux first"<<endl;
      exit(3);
    }
  }
  
  for(int i=0;i<(int)color2.size();i++){
    found=0;
    for(int j=0;j<(int)allfluxes.size();j++){
      if(color2[i] == allfluxes[j]){
	found=1;
	//assign index
	color2_ind.push_back(fluxid[j]);
      }
    }
    //if not found error
    if(found !=1){
      cout<<"HISTO_INIT: Error!!! To compute color with "<<color2[i]
	  <<" you must set the flux first"<<endl;
      exit(3);
    }
  }

  
 for(int ll=0;ll<(int)color1.size();ll++){
  for(int j12=0;j12<(long)allfluxes.size();j12++){
     if(allfluxes[j12].compare(color1[ll]) == 0) color1_ind[ll]=j12;
     if(allfluxes[j12].compare(color2[ll]) == 0) color2_ind[ll]=j12;
  }
}

//Now deal with the flux and color range and binsize
//for the color-magnitude diagrams  


  //DO min flux
  f2=outminflux.find(",");
  f1=0;
  end=outminflux.size()-1;
  if(f2 > end) stop=1;
  else stop=0;
  
  
  while(f2){
    //extract substring
    thisflux=outminflux.substr(f1,f2);
    outminflux.assign(outminflux.substr(f2+1,end));
    
    //update the out flux
    allminflux.push_back(atof(thisflux.data()));
    
    //update
    end=outminflux.size()-1;
    f2=outminflux.find(",");
    if(stop == 1) break;
    if(f2>end)stop=1;
  };
  
  
  //Do max flux
  f2=outmaxflux.find(",");
  f1=0;
  end=outmaxflux.size()-1;
  if(f2 > end) stop=1;
  else stop=0;
  
  
  while(f2){
    //extract substring
    thisflux=outmaxflux.substr(f1,f2);
    outmaxflux.assign(outmaxflux.substr(f2+1,end));
    
    //update the out flux
    allmaxflux.push_back(atof(thisflux.data()));
     
    //update
    end=outmaxflux.size()-1;
    f2=outmaxflux.find(",");
    if(stop == 1) break;
    if(f2>end)stop=1;
  };
  
  
  //Do min color
  f2=outmincolor.find(",");
  f1=0;
  end=outmincolor.size()-1;
  if(f2 > end) stop=1;
  else stop=0;
  
  
  while(f2){
    //extract substring
    thisflux=outmincolor.substr(f1,f2);
    outmincolor.assign(outmincolor.substr(f2+1,end));
    
    //update the out flux
    allmincolor.push_back(atof(thisflux.data()));
    
    //update
    end=outmincolor.size()-1;
    f2=outmincolor.find(",");
    if(stop == 1) break;
    if(f2>end)stop=1;
  };
  
   //Do max color
  f2=outmaxcolor.find(",");
  f1=0;
  end=outmaxcolor.size()-1;
  if(f2 > end) stop=1;
  else stop=0;
  
  
  while(f2){
    //extract substring
    thisflux=outmaxcolor.substr(f1,f2);
    outmaxcolor.assign(outmaxcolor.substr(f2+1,end));
    
    //update the out flux
    allmaxcolor.push_back(atof(thisflux.data()));
    
    //update
    end=outmaxcolor.size()-1;
    f2=outmaxcolor.find(",");
    if(stop == 1) break;
    if(f2>end)stop=1;
  };
  
  
  //check if max min color flux matches with numbers  of colors and fluxes
  if(allmaxcolor.size() != color1.size() || allmincolor.size() != color1.size()){
    cout<<"HISTO_INIT: Error!!! Color requests and limits do not match!"<<endl;
    exit(3);
  }
  
  if(allmaxflux.size() != color1.size() || allminflux.size() != color1.size()){
    cout<<"HISTO_INIT: Error!!! Flux requests and limits do not match!"<<endl;
    exit(3);
  }
 

  return;
}
